package api

import (
	"encoding/json"
	"errors"
	"fmt"
	"hft/Database/admin/user_db"
	wx_template "hft/e/template"
	wx_app_id "hft/e/wx_app_conf"
	"hft/tools/weixin"
	"hft/tools/weixin/model"
	"net/url"
	"time"

	"hft/e"

	"github.com/tidwall/gjson"
)

type Template struct{}

// 获取模板列表
func (*Template) List() (list []*model.TemplateModel, err error) {
	fmt.Println("1111111111111111")
	data := weixin.WeiXinBase.BaseRequestV2("GET", "/cgi-bin/template/get_all_private_template", &url.Values{}, []byte{})
	var res = string(data)
	fmt.Println("模板回复", res)
	errcode := gjson.Get(res, "errcode").String()
	if errcode != "" {
		return nil, errors.New(e.ErrorInfo)
	}
	var newList = gjson.Get(res, "template_list").String()
	var templateList []*model.TemplateModel
	err = json.Unmarshal([]byte(newList), &templateList)
	if err != nil {
		return nil, errors.New(e.ErrorInfo)
	}
	return templateList, nil
}

// 发送客户接待提醒线索模板消息
func (*Template) SendClueMessage(toUserOpenID string, user *user_db.User, lou_name string) {
	// var pagePath string = ""
	// if user.ManagerID == 0 {
	// 	pagePath = wx_app_id.CustomerPath
	// } else {
	// 	pagePath = wx_app_id.CustomerDetail + fmt.Sprintf("?id=%v&is_mine=1", user.ID)
	// }
	var clueMessage = &model.ClueTemplate{
		BaseTemplate: model.BaseTemplate{
			Touser:     toUserOpenID,
			TemplateID: wx_template.CLUE_REMIND,
			Miniprogram: model.Miniprogram{
				Appid: wx_app_id.AppID,
				// PagePath: pagePath,
			},
		},
		Data: model.ClueData{
			PhoneNumber2: model.BaseValue{Value: user.Phone},
			Thing6:       model.BaseValue{Value: lou_name},
			Time10:       model.BaseValue{Value: time.Now().Format("2006年01月02日 15:04:05")},
		},
	}
	var data, _ = json.Marshal(&clueMessage)
	// fmt.Println(string(data))
	res := weixin.WeiXinBase.BaseRequestV2("POST", "/cgi-bin/message/template/send", &url.Values{}, data)
	fmt.Println(string(res))
}

var WeiXinTemplate = new(Template)
